package com.maxjay.system.web.controller.struts;

import java.util.HashSet;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;

import com.maxjay.common.exception.ERR;
import com.maxjay.common.exception.GeneException;
import com.maxjay.common.util.date.DateUtil;
import com.maxjay.system.po.BaseEntity;
import com.maxjay.system.service.BaseService;
import com.opensymphony.xwork2.interceptor.annotations.Before;

/**
 * 业务对象增加、更新基础控制类
 * 
 * @author Administrator
 *
 */
@SuppressWarnings({"rawtypes"})
public class BaseBOController extends BaseController {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	protected String updateKey;
	
	private Set<String> dateProperties = new HashSet<String>();
	
	@Resource(name = "baseService")
	private BaseService baseService;
	
	protected BaseService getBaseService(){
		return baseService;
	}

	protected void saveOrUpdate(BaseEntity baseEntityVo) {
		if(StringUtils.isNotBlank(baseEntityVo.getObjId())){
			getBaseService().update(baseEntityVo);
		} else {
			getBaseService().save(baseEntityVo);
		}
	}
	
	protected void saveOrValidateUpdate(BaseEntity baseEntityVo) {
		if(StringUtils.isNotBlank(baseEntityVo.getObjId())){
			validateUpdate(baseEntityVo);
		} else {
			getBaseService().save(baseEntityVo);
		}
	}
	
	private void validateUpdate(BaseEntity baseEntityVo){
		getBaseService().validateUpdate(baseEntityVo, updateKey);
	}
	
	protected void delete(Class clazz, String objId) {
		baseService.delete(clazz, objId);
	}

	protected void validateDelete(Class clazz, String objId) {
		baseService.validateDelete(clazz, objId, updateKey);
	}
	
	@Override
	@Before
	public void beforeExecute() {
		super.beforeExecute();
		this.configDateProperties();
		this.formatDateProperties(DateUtil.DB_STORE_DATE_SPLIT);
	}
	
	/**
	 * 格式化时间类型属性
	 * 
	 * @param formatType 目标转换类型  可从DateUtil中取得
	 */
	protected void formatDateProperties(int formatType) {
		try {
			for(String dateProperty : dateProperties) {
				Object obj = this;
				String[] properties = dateProperty.split("\\.");
				for(int i=0; i < properties.length; i++) {
					if(i+1 == properties.length) {
						Object propValue = PropertyUtils.getProperty(obj, properties[i]);
						if(!(propValue instanceof String)) {
							throw GeneException.getGeneException(ERR.ERROR_ERR_MSG, "属性[" + dateProperty + "]不是String类型，无法进行时间格式转换");
						}
						String formatStr = DateUtil.getDateStrByStr((String)propValue, formatType);
						PropertyUtils.setProperty(obj, properties[i], formatStr);
					} else {
						obj = PropertyUtils.getProperty(obj, properties[i]);
						if(obj == null) {
							break;
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw GeneException.getGeneException(ERR.ERROR_ERR_MSG, e);
		}
	}
	
	@Override
	protected void configPropertiesInViewState() {
		super.configPropertiesInViewState();
		this.addPropertiesInViewState("updateKey");
	}
	
	protected void configDateProperties() {
		
	}
	
	protected void addDateProperties(String...properties) {
		for(String property : properties) {
			dateProperties.add(property);
		}
	}
	
	public String getUpdateKey() {
		return updateKey;
	}

	public void setUpdateKey(String updateKey) {
		this.updateKey = updateKey;
	}
	
}